import { join } from 'path';
import * as fs from 'node:fs';

const sqlDir = join(process.cwd(), 'sql');
const sqlCache = Object.fromEntries(
  fs
    .readdirSync(sqlDir)
    .filter((file) => file.endsWith('.sql'))
    .map((file) => [file, fs.readFileSync(join(sqlDir, file), 'utf8')]),
);

export function loadSqlFile(name: string) {
  return sqlCache[name];
}

export function renderTemplate(
  template: string,
  context: Record<string, any>,
): string {
  return template.replace(/\$\{([\w_]+)}/g, (_, key) => {
    if (key in context) {
      return context[key];
    }
    return ''; // 或抛出错误
  });
}
